#include <assert.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include "scene.h"
#include "ctrlboard.h"
#include "mg_Lcd_Driver.h"
#include "mg_Device.h"
#include "mg_Rs485Protocol.h"
/* widgets:
Layer_KaiHeLian
Background_KaiHeLian
Container_KaiHeLian_On
Container_KaiHeLian_Text
Text_KaiHeLian_Baifenhao
Text_KaiHeLian_Liangdu
Container_KaiHeLian_Button
PopupButton_KaiHeLian_Nouse
Meter_KaiHeLian_Xuanzhuan
Icon_KaiHeLian_Pointer
CircleProgressBar_KaiHeLian_Temp
Icon_KaiHeLian_Jingdu
Text_KaiHeLian_XingChengNum
Button_Back_MainMenu
*/
static ITUBackground* Background_KaiHeLian;
static ITUMeter* Meter_KaiHeLian_Xuanzhuan;
static ITUText* Text_KaiHeLian_Baifenhao;
static ITUText* Text_KaiHeLian_Xingcheng;
bool KaiHeLianOnEnter(ITUWidget* widget, char* param)
{
	if (!Background_KaiHeLian)//首次加载
	{
		Background_KaiHeLian = ituSceneFindWidget(&theScene, "Background_KaiHeLian");
		assert(Background_KaiHeLian);
		Meter_KaiHeLian_Xuanzhuan = ituSceneFindWidget(&theScene, "Meter_KaiHeLian_Xuanzhuan");
		assert(Meter_KaiHeLian_Xuanzhuan);
		Text_KaiHeLian_Baifenhao = ituSceneFindWidget(&theScene, "Text_KaiHeLian_Baifenhao");
		assert(Text_KaiHeLian_Baifenhao);
		Text_KaiHeLian_Xingcheng = ituSceneFindWidget(&theScene, "Text_KaiHeLian_Xingcheng");
		assert(Text_KaiHeLian_Xingcheng);
	}
	ituMeterSetValue(Meter_KaiHeLian_Xuanzhuan, TFT_Device_State[DisplayValue.TFT_Display_Device].IT_Dev_STatus_Value.IT_Motor_Status.route);

	//对标题进行语言转换
	if (Sys.Language == MG_LANGUAGE_CHS || Sys.Language == MG_LANGUAGE_ENG)
	{
		Text_KaiHeLian_Xingcheng->fontIndex = 0;
		Text_KaiHeLian_Xingcheng->boldSize = 1;
		ituTextSetFontSize(Text_KaiHeLian_Xingcheng, 18);
	}
	else if (Sys.Language == MG_LANGUAGE_ZHTW)
	{
		Text_KaiHeLian_Xingcheng->fontIndex = 8;
		Text_KaiHeLian_Xingcheng->boldSize = 0;
		ituTextSetFontSize(Text_KaiHeLian_Xingcheng, 16);
	}
	//对“%”坐标判断
	if (Meter_KaiHeLian_Xuanzhuan->value >= 100)
		ituWidgetSetX(Text_KaiHeLian_Baifenhao, 141);
	else
		ituWidgetSetX(Text_KaiHeLian_Baifenhao, 123);

	Set_Rotate_Block(30);//旋转步进
	return true;
}

bool KaiHeLianOnLeave(ITUWidget* widget, char* param)
{
	MessageFlag[MSG_GET_DEVICE_STATE_EN] = ON;//允许10s自动获取状态
    return true;
}

bool KaiHeLianOnTimer(ITUWidget* widget, char* param)
{
	static int MeterValue = 0, MeterValue_L = 0;
	static uint32_t SendEndTime = Rotate_SendCmd_Time, SendTime = 0;
	static bool Is_Rotate = false;
	//用户手势操作
	if (Action_Cmd.User_Action == Left_Slide || Action_Cmd.User_Action == Right_Slide)
	{
		GotoMainMenu();//返回主菜单
		Clear_User_Action();
	}

	//设备操作
	MeterValue = Meter_KaiHeLian_Xuanzhuan->value;
	if (Action_Cmd.User_Action == Clockwise_Rotation && MeterValue < 100)//有用户操作
	{
		MeterValue += Action_Cmd.Action_Times;
		if (MeterValue > 100)MeterValue = 100;
		ituMeterSetValue(Meter_KaiHeLian_Xuanzhuan, MeterValue);//设置Meter百分比值
		MessageFlag[MSG_GET_DEVICE_STATE_EN] = OFF;//关闭10s自动获取状态
		Is_Rotate = true;
		Clear_User_Action();
	}
	else if (Action_Cmd.User_Action == Anticlockwise_Rotation && MeterValue > 0)
	{
		MeterValue -= Action_Cmd.Action_Times;
		if (MeterValue < 0)MeterValue = 0;
		ituMeterSetValue(Meter_KaiHeLian_Xuanzhuan, MeterValue);//设置Meter百分比值
		MessageFlag[MSG_GET_DEVICE_STATE_EN] = OFF;//关闭10s自动获取状态
		Is_Rotate = true;
		Clear_User_Action();
	}

	//显示状态更新
	if (MessageFlag[MSG_GUI_STATE_UPDATE] == ON)
	{
		ituMeterSetValue(Meter_KaiHeLian_Xuanzhuan, TFT_Device_State[DisplayValue.TFT_Display_Device].IT_Dev_STatus_Value.IT_Motor_Status.route);
		MeterValue = Meter_KaiHeLian_Xuanzhuan->value;
		MeterValue_L = MeterValue;
		SendEndTime = Rotate_SendCmd_Time + 1;
		MessageFlag[MSG_GUI_STATE_UPDATE] = OFF;
	}
	else
	{
		//发送执行报文
		if (MeterValue == MeterValue_L)
		{
			if (SendEndTime <= Rotate_SendCmd_Time)
				SendEndTime++;
			//停止操作0.6s后发送执行报文
			if (SendEndTime == Rotate_SendCmd_Time)
			{
				SendTime = 0;
				RS485_Set_TFTDeviceState(DisplayValue.TFT_Display_Device);
				Is_Rotate = false;
			}
		}
		else
		{
			SendEndTime = 0;
			TFT_Device_State[DisplayValue.TFT_Display_Device].IT_Dev_STatus_Value.IT_Motor_Status.route = MeterValue;
			TFT_Device_Cmd[DisplayValue.TFT_Display_Device].IT_Dev_STatus_Value.IT_Motor_Status.status = 0x14;//命令设置行程
			TFT_Device_Cmd[DisplayValue.TFT_Display_Device].IT_Dev_STatus_Value.IT_Motor_Status.route = MeterValue;
		}
		//当前是否在旋转动作中
		if (Is_Rotate)
		{
			if (SendTime < Rotating_SendCmd_Time)
				SendTime++;
			if (SendTime == Rotating_SendCmd_Time)//0.495s发送一次命令
			{
				SendTime = 0;
				RS485_Set_TFTDeviceState(DisplayValue.TFT_Display_Device);
			}
		}
	}
	//对“%”坐标判断
	if (Meter_KaiHeLian_Xuanzhuan->value >= 100)
		ituWidgetSetX(Text_KaiHeLian_Baifenhao, 141);
	else
		ituWidgetSetX(Text_KaiHeLian_Baifenhao, 123);
	//显示状态更新
	if (MessageFlag[MSG_GUI_STATE_UPDATE] == ON)
	{
		ituMeterSetValue(Meter_KaiHeLian_Xuanzhuan, TFT_Device_State[DisplayValue.TFT_Display_Device].IT_Dev_STatus_Value.IT_Motor_Status.route);
		MeterValue = Meter_KaiHeLian_Xuanzhuan->value;
		MessageFlag[MSG_GUI_STATE_UPDATE] = OFF;
	}
	MeterValue_L = MeterValue;

    return true;
}

